package com.roger.exercise;

import java.io.ByteArrayOutputStream;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.util.List;

import android.app.Activity;
import android.content.Intent;
import android.content.SharedPreferences;
import android.content.res.Resources.NotFoundException;
import android.net.Uri;
import android.os.Bundle;
import android.telephony.SmsManager;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.EditText;
import android.widget.Toast;

public class RogerActivity extends Activity {
	/** Called when the activity is first created. */
	@Override
	public void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.main);

		final SharedPreferences preferences = getSharedPreferences("history",
				MODE_PRIVATE);
		final SharedPreferences.Editor editor = preferences.edit();

		// show last phoneNumber、smsContent from SharedPreference
		final String phoneNumber = preferences.getString("phoneNumber", null);
		if (phoneNumber != null && phoneNumber.length() > 0) {
			((EditText) findViewById(R.id.phoneNumberText))
					.setText(phoneNumber);
		}
		final String smsContent = preferences.getString("smsContent", null);
		if (smsContent != null && smsContent.length() > 0) {
			((EditText) findViewById(R.id.smsContentText)).setText(smsContent);
		}

		// show last fileName、fileContent from SQLiteDB
		RogerDBService rogerDBService = new RogerDBService(getBaseContext());
		RogerInfo rogerInfo = rogerDBService.findById(1);
		String fileName = rogerInfo.getFileName();
		if (fileName != null && fileName.length() > 0) {
			((EditText) findViewById(R.id.fileNameText)).setText(fileName);
		}
		String fileContent = rogerInfo.getFileContent();
		if (fileContent != null && fileContent.length() > 0) {
			((EditText) findViewById(R.id.fileContentText))
					.setText(fileContent);
		}

		// 打电话
		Button dailButton = (Button) this.findViewById(R.id.dailButton);
		dailButton.setOnClickListener(new OnClickListener() {
			@Override
			public void onClick(View arg0) {
				String phoneNumber = ((EditText) findViewById(R.id.phoneNumberText))
						.getText().toString();
				if (phoneNumber == null || phoneNumber.length() == 0) {
					Toast.makeText(RogerActivity.this,
							R.string.toast_enter_phone_number,
							Toast.LENGTH_LONG).show();
				} else {
					editor.putString("phoneNumber", phoneNumber);
					Intent intent = new Intent();
					intent.setAction(Intent.ACTION_CALL);
					intent.setData(Uri.parse("tel:" + phoneNumber));
					startActivity(intent);
				}
			}
		});
		// 发短信
		Button smsSendButton = (Button) this.findViewById(R.id.smsSendButton);
		smsSendButton.setOnClickListener(new OnClickListener() {
			@Override
			public void onClick(View arg0) {
				try {
					String phoneNumber = ((EditText) findViewById(R.id.phoneNumberText))
							.getText().toString();
					if (phoneNumber == null || phoneNumber.length() == 0) {
						Toast.makeText(RogerActivity.this,
								R.string.toast_enter_phone_number,
								Toast.LENGTH_LONG).show();
						return;
					}
					editor.putString("phoneNumber", phoneNumber);
					String smsContent = ((EditText) findViewById(R.id.smsContentText))
							.getText().toString();
					if (smsContent == null || smsContent.length() == 0) {
						Toast.makeText(RogerActivity.this,
								R.string.toast_enter_sms_content,
								Toast.LENGTH_LONG).show();
						return;
					}
					editor.putString("smsContent", smsContent);
					List<String> smss = SmsManager.getDefault().divideMessage(
							smsContent);
					for (String sms : smss) {
						SmsManager.getDefault().sendTextMessage(phoneNumber,
								null, sms, null, null);
					}
					Toast.makeText(RogerActivity.this, R.string.smsSendSuccess,
							Toast.LENGTH_LONG).show();
				} catch (NotFoundException e) {
					e.printStackTrace();
					Toast.makeText(RogerActivity.this, R.string.smsSendFailed,
							Toast.LENGTH_LONG).show();
				}
			}
		});
		// 文件保存
		Button saveFileButton = (Button) this.findViewById(R.id.saveFileButton);
		saveFileButton.setOnClickListener(new OnClickListener() {
			@Override
			public void onClick(View arg0) {
				String fileName = null;
				String fileContent = null;
				try {
					fileName = ((EditText) findViewById(R.id.fileNameText))
							.getText().toString();
					if (fileName == null || fileName.length() == 0) {
						Toast.makeText(RogerActivity.this,
								R.string.toast_enter_file_name,
								Toast.LENGTH_LONG).show();
						return;
					}
					fileContent = ((EditText) findViewById(R.id.fileContentText))
							.getText().toString();
					if (fileContent == null || fileContent.length() == 0) {
						Toast.makeText(RogerActivity.this,
								R.string.toast_enter_file_content,
								Toast.LENGTH_LONG).show();
						return;
					}

					byte[] fileContentByteArray = fileContent.getBytes("UTF-8");
					FileOutputStream fos = openFileOutput(fileName,
							MODE_PRIVATE);
					fos.write(fileContentByteArray, 0,
							fileContentByteArray.length);
					fos.close();
					Toast.makeText(RogerActivity.this,
							R.string.saveFileSuccess, Toast.LENGTH_LONG).show();
				} catch (Exception e) {
					e.printStackTrace();
					Toast.makeText(RogerActivity.this, R.string.saveFileFailed,
							Toast.LENGTH_LONG).show();
				}

				// 文件保存到数据库
				RogerInfo rogerInfo = new RogerInfo();
				rogerInfo.setId(1);
				rogerInfo.setFileName(fileName);
				rogerInfo.setFileContent(fileContent);

				RogerDBService rogerDBService = new RogerDBService(
						getBaseContext());
				rogerDBService.save(rogerInfo);
			}
		});

		// 文件读取
		Button readFileButton = (Button) this.findViewById(R.id.readFileButton);
		readFileButton.setOnClickListener(new OnClickListener() {
			@Override
			public void onClick(View v) {
				try {
					String fileName = ((EditText) findViewById(R.id.fileNameText))
							.getText().toString();
					if (fileName == null || fileName.length() == 0) {
						Toast.makeText(RogerActivity.this,
								R.string.toast_enter_file_name,
								Toast.LENGTH_LONG).show();
						return;
					}
					FileInputStream fis = openFileInput(fileName);
					ByteArrayOutputStream baos = new ByteArrayOutputStream();
					byte[] buffer = new byte[4096];
					int len = 0;
					while ((len = fis.read(buffer)) != -1) {
						baos.write(buffer, 0, len);
					}
					baos.close();
					fis.close();
					byte[] bufferResult = baos.toByteArray();
					String content = new String(bufferResult, "UTF-8");
					EditText fileContentText = (EditText) findViewById(R.id.fileContentText);
					fileContentText.setText(content);
					Toast.makeText(RogerActivity.this,
							R.string.readFileSuccess, Toast.LENGTH_LONG).show();
				} catch (Exception e) {
					e.printStackTrace();
					Toast.makeText(RogerActivity.this, R.string.readFileFailed,
							Toast.LENGTH_LONG).show();
				}
			}
		});
	}
}